package droid.frame.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * TextView(正方形)<br>
 * 控件大小根据"背景图片"的原始长宽等比例缩放 （即是:高度/宽度==1, 高度决定宽度）
 * 
 * @author coffee<br>
 *         2015-8-5下午2:56:16
 */
@SuppressLint("AppCompatCustomView")
public class SquareTextView extends TextView {
	private int viewWidth = 0;//
	private int viewHeight = 0;

	public SquareTextView(Context context) {
		super(context);
	}

	public SquareTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
		// 组件的高度
		viewHeight = getBackground().getIntrinsicHeight() + getPaddingTop() + getPaddingBottom() + 4;
		// 屏幕的宽度根据组件
		viewWidth = viewHeight;
		// Log.d("image:scale", imageWidth + "," + imageHeight + " -- " + viewWidth + "," + viewHeight);
		widthMeasureSpec = MeasureSpec.makeMeasureSpec(viewWidth, MeasureSpec.EXACTLY);
		// 精确计算高度
		heightMeasureSpec = MeasureSpec.makeMeasureSpec(viewHeight, MeasureSpec.EXACTLY);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
}
